<?php

class COrder extends Zend_Db_Table{
	
	protected $_name;
	
	protected $_primary;

	
	public function _setup(){
		
		$this->_name=R_PRE."orders";
		
		$this->_primary="orderid";
		
		parent::_setup();
		
	}
	public function getOrderByUid($uid){
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where($this->_name.'.uid = ? ',$uid);
		
		$select .=" and flid in (2,3,4,5,9)";
		return $db->fetchAll($select);
	}
	public function createOrder($data){
		
		$orderid=$this->insert($data);

		return $orderid;
		
	}
	
	public function editOrder($orderid,$data){
		
		$db=$this->getAdapter();
		
		$where = $db->quoteInto('orderid = ?', $orderid);

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			
			return false;
			
		}else{
			if($data['flid'] == 7) {
				$this->updateOrderStock($orderid);
			}
			return true;
			
		}
		
	}
	
	public function editOrderArray($ordeidrArray,$data){
		
		$db=$this->getAdapter();
		
		if(is_array($ordeidrArray)){
		
			$whereString=join(',',$ordeidrArray);
			
			$where = "orderid IN ($whereString)";
		
		}else{
			
			$where = "orderid IN ($ordeidrArray)";
			
		}

		$rows_affected=$db->update($this->_name,$data,$where);
		
		if($rows_affected!=1){
			return false;
			
		}else{
			if($data['flid'] == 7) {
				foreach ($ordeidrArray as $id) {
					if($id == '')
						continue;
					$this->updateOrderStock($orderid);
				}
			}
			return true;
			
		}	
		
	}
	
	public function getDetail($orderid){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
		
		$select->where($this->_name.'.orderid = ? ',$orderid);
		
		$select->joinLeft(R_PRE.'members',R_PRE.'members.uid='.$this->_name.'.uid');
		
		$select->joinLeft(R_PRE.'members AS A','A.uid='.$this->_name.'.seller_uid','A.username AS seller_name');
		
		$select->joinLeft(R_PRE.'orderfls',"$this->_name.flid=".R_PRE.'orderfls.flid');
		
		return $db->fetchAll($select);
		
	}
	
	public function delOrders($orderidArray){
		
		$db=$this->getAdapter();
		
		$orderString=join(',',$orderidArray);
		
		$result=$db->query("DELETE FROM $this->_name WHERE orderid IN ($orderString)");
		
		return $result;
		
	}
	
	
	
	public function getAllOrderCounts($uid=0,$where='',$seller_uid=0, $contact_name=''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();

		$select->from($this->_name,$this->_name.'.orderid');
		
		if($where!=''){
			
			$select->where($where);
	
		}
		
		if($uid!=0&&$uid!=''){
			
			$select->where('uid = ? ',$uid);
			
		}
		
		if($seller_uid!=0&&$seller_uid!=''){
			
			$select->where('seller_uid = ? ',$seller_uid);
			
		}
		
		if($contact_name != '') {
			$select->joinLeft(R_PRE.'order_contacts',"$this->_name.contactid=".R_PRE.'order_contacts.contactid',R_PRE.'order_contacts.name');
			$select->where(R_PRE.'order_contacts.name=?',$contact_name);
		}
		
		return count($db->fetchAll($select));
		
	}
	
	public function getAllOrders($from,$counts,$uid=0,$where='',$seller_uid=0, $contact_name = ''){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');

		if($uid!=0&&$uid!=''){
			
			$select->where($this->_name.'.uid = ? ',$uid);
			
		}
			
		$select->joinLeft(R_PRE.'members', R_PRE.'members.uid = '.$this->_name.'.uid', '*');

		if($seller_uid!=0&&$seller_uid!=''){
			
			$select->where('seller_uid = ? ',$seller_uid);
			
		}
		
		$select->joinLeft(R_PRE.'members AS A', 'A.uid = '.$this->_name.'.seller_uid', 'A.username AS seller_name');	

		$select->joinLeft(R_PRE.'orderfls',"$this->_name.flid=".R_PRE.'orderfls.flid');
		
		if($where!=''){
			
			$select->where($where);
			
		}
		
		$select->order("$this->_name.add_time DESC");
		
		if($counts!=0){
		
			$select->limit($counts, $from);
		
		}
		
		if($contact_name != '') {
			$select->joinLeft(R_PRE.'order_contacts',"$this->_name.contactid=".R_PRE.'order_contacts.contactid',R_PRE.'order_contacts.name');
			$select->where(R_PRE.'order_contacts.name=?',$contact_name);
		}
		
		return $db->fetchAll($select);
		
	}
	
	public function updateDeliveryDay($delivery_days,$orderid,$isPlus){
		
		$db=$this->getAdapter();
		
		try{
			
			if($isPlus){
		
				$db->query("UPDATE $this->_name SET delivery_days=delivery_days+$delivery_days WHERE orderid='$orderid'");
				
			}else{
				
				$db->query("UPDATE $this->_name SET delivery_days=delivery_days-$delivery_days WHERE orderid='$orderid'");
				
			}
			
			return true;
		
		}catch(Exception $e){
			
			return $e->getMessage();
			
		}
		
		return false;
			
	}
	
	public function getAllOrdersByIds($ids){
		
		$db=$this->getAdapter();
		
		$select = $db->select();
		
		$select->from($this->_name,'*');
			
		$select->joinLeft(R_PRE.'members', R_PRE.'members.uid = '.$this->_name.'.uid', '*');
		
		$select->joinLeft(R_PRE.'members AS A', 'A.uid = '.$this->_name.'.seller_uid', 'A.username AS seller_name');	

		$select->joinLeft(R_PRE.'orderfls',"$this->_name.flid=".R_PRE.'orderfls.flid');
		
		if($ids!=''){
			
			$select->where("$this->_name.orderid in ($ids)");
			
		}
		
		$select->order("$this->_name.add_time DESC");
		
		return $db->fetchAll($select);
		
	}
	
	public function updateOrderStock($orderid)
	{
		$order_detail = $this->getDetail($orderid);
		$shopingArray = unserialize($order_detail[0]['order_string']);
		if(is_array($shopingArray) && $shopingArray != null) {
			$product = new CProduct();
			foreach ($shopingArray as $value) {
				if($value['pattern_name'] == 'item' && $value['itemid'] != '') {
					$product->updateStock("+{$value['amounts']}", $value['itemid']);
				}
			}
		}
	}
	
}